package com.phone.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.phone.entity.CartItml;
import com.phone.entity.Product;
import com.phone.entity.User;
import com.phone.service.CartService;
import com.phone.service.ProductService;
import com.phone.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;


@Controller
public class CartController {
	@Resource
	private UserService userServiceImpl;
	@Resource
	private ProductService productServiceImpl;
	@Resource
	private CartService cartServiceImpl;
	
	
	@RequestMapping("savecart")
	public String savecart(HttpSession session,HttpServletRequest request){
//		    User u=(User) session.getAttribute("user");
//		    String username=u.getUsername();
		    String username=request.getParameter("username");
		    User user = userServiceImpl.seleu(username);
		    System.out.println(user);
			String ids = request.getParameter("id");
			int id=Integer.parseInt(ids);
			Product product = productServiceImpl.seleproduct(id);
			CartItml cart=new CartItml();
			int count=1;
			int price = Integer.parseInt(product.getPrice());
			int totalprice=count*price;
			cart.setCount(count);
			cart.setDescription(product.getDescription());
			cart.setListimg(product.getListimg());
			cart.setName(product.getName());
			cart.setPrice(price);
			cart.setTotalprice(totalprice);
			cart.setUser(user);
			cartServiceImpl.savecart(cart);
			session.setAttribute("message", "添加成功!");
			List<Product> products = productServiceImpl.allproduct();
			session.setAttribute("products", products);
			return "/usershop.jsp";
	}
	@RequestMapping("showcart")
	public String showcart(Model model,HttpSession session,HttpServletRequest request){
		String username = request.getParameter("username");
		User user = userServiceImpl.seleu(username);//获取用户id
		int uid=user.getId();
		List<CartItml> showcart = cartServiceImpl.showcart(uid);
		if (showcart==null || showcart.size()==0) {
			session.setAttribute("message", "你的购物车没有东西啦!");
			return "allproduct";
		}else {
			model.addAttribute("showcart", showcart);
			model.addAttribute("user", user);
			return "/cart.jsp";
		}
	}
	@RequestMapping("delcart")
	public String delcart(HttpServletRequest request,HttpSession session,Model model){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		cartServiceImpl.delcart(id);
//		User u=(User) session.getAttribute("user");
//		String username=u.getUsername();
		String username = request.getParameter("username");
		User user = userServiceImpl.seleu(username);//获取id
		int uid=user.getId();
		List<CartItml> showcart = cartServiceImpl.showcart(uid);
		if (showcart==null || showcart.size()==0) {
			session.setAttribute("message", "你的购物车没有东西啦!");
			List<Product> products = productServiceImpl.allproduct();
			session.setAttribute("user", user);
			model.addAttribute("products", products);
			return "/usershop.jsp";
		}else {
			model.addAttribute("showcart", showcart);
			model.addAttribute("user", user);
			return "/cart.jsp";
		}
	}
	@RequestMapping("acart")
	public String acart(HttpSession session){
		session.setAttribute("message", "你还没有登录!");
		return "/signin.jsp";
	}
	@RequestMapping("addcart")
	public String addcart(HttpServletRequest request,Model model,CartItml cartItml,HttpSession session){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		cartServiceImpl.upcart(id);
//		User u=(User) session.getAttribute("user");
//		String username=u.getUsername();
		String username = request.getParameter("username");
		User user = userServiceImpl.seleu(username);//获取id
		int uid=user.getId();
		List<CartItml> showcart = cartServiceImpl.showcart(uid);
		model.addAttribute("showcart", showcart);
		model.addAttribute("user", user);
		return "/cart.jsp";
	}
	@RequestMapping("clear")
	public String clear(HttpServletRequest request,HttpSession session,Model model){
		String username = request.getParameter("username");
		User user = userServiceImpl.seleu(username);//获取id
		int uid=user.getId();
		List<CartItml> list = cartServiceImpl.showcart(uid);
		for(int i=0;i<list.size();i++){
			CartItml cartItml = list.get(i);
			cartServiceImpl.clear(cartItml);
		}
		session.setAttribute("message", "你的购物车没有东西啦!");
		List<Product> products = productServiceImpl.allproduct();
		session.setAttribute("user", user);
		model.addAttribute("products", products);
		return "/usershop.jsp";
	}
}
